package com.eedi.framework.dept.controller.sys;

import com.eedi.framework.dept.controller.sys.vo.*;
import com.eedi.framework.dept.controller.sys.vo.*;
import com.eedi.framework.dept.dal.dataobject.SysDeptDO;
import com.eedi.framework.dept.service.SysDeptService;
import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.pojo.CommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.DeleteMapping;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import org.springframework.web.bind.annotation.*;

import static com.eedi.framework.common.pojo.CommonResult.success;

import com.eedi.framework.common.util.object.BeanUtils;

@Tag(name = "系统界面 - 部门")
@RestController
@RequestMapping("/dept")
@Validated
public class SysDeptController {

    @Resource
    private SysDeptService sysDeptService;

    @PostMapping("create")
    @Operation(summary = "创建部门")
//    @PreAuthorize("@ss.hasPermission('sys:dept:create')")
    public CommonResult<String> createDept(@Valid @RequestBody SysDeptSaveReq createReqVO) {
        String deptId = sysDeptService.createDept(createReqVO);
        return success(deptId);
    }

    @PutMapping("update")
    @Operation(summary = "更新部门")
//    @PreAuthorize("@ss.hasPermission('sys:dept:update')")
    public CommonResult<Boolean> updateDept(@Valid @RequestBody SysDeptUpReq updateReqVO) {
        sysDeptService.updateDept(updateReqVO);
        return success(true);
    }

    @DeleteMapping("sysDeptId")
    @Operation(summary = "删除部门")
    @Parameter(name = "sysDeptId", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('sys:dept:delete')")
    public CommonResult<Boolean> deleteDept(@RequestParam("sysDeptId") String sysDeptId) {
        sysDeptService.deleteDept(sysDeptId);
        return success(true);
    }

    @DeleteMapping("disable")
    @Operation(summary = "停用部门")
    @Parameter(name = "sysDeptId", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('sys:dept:delete')")
    public CommonResult<Boolean> disable(@RequestParam("sysDeptId") String sysDeptId) {
        sysDeptService.disable(sysDeptId);
        return success(true);
    }

    @DeleteMapping("enable")
    @Operation(summary = "启用部门")
    @Parameter(name = "sysDeptId", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('sys:dept:delete')")
    public CommonResult<Boolean> enable(@RequestParam("sysDeptId") String sysDeptId) {
        sysDeptService.enable(sysDeptId);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "获取部门列表")
//    @PreAuthorize("@ss.hasPermission('sys:dept:query')")
    public CommonResult<List<SysDeptResp>> getDeptList(SysDeptListReq reqVO) {
        List<SysDeptDO> list = sysDeptService.getDeptList(reqVO);
        return success(BeanUtils.toBean(list, SysDeptResp.class));
    }

    @GetMapping(value = {"/list-all-simple"})
    @Operation(summary = "获取部门精简信息列表", description = "只包含被开启的部门，主要用于前端的下拉选项")
    public CommonResult<List<SysDeptSimpleResp>> getSimpleDeptList() {
        SysDeptListReq sysDeptListReq = new SysDeptListReq();
        sysDeptListReq.setSysDeptStatus(CommonStatusEnum.ENABLE);
        List<SysDeptDO> list = sysDeptService.getDeptList(sysDeptListReq
        );
        return success(BeanUtils.toBean(list, SysDeptSimpleResp.class));
    }

    @GetMapping("/get")
    @Operation(summary = "获得部门信息")
    @Parameter(name = "sysDeptId", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('sys:dept:query')")
    public CommonResult<SysDeptResp> getDept(@RequestParam("sysDeptId") String sysDeptId) {
        SysDeptDO dept = sysDeptService.getDept(sysDeptId);
        return success(BeanUtils.toBean(dept, SysDeptResp.class));
    }

}
